package com.xunshangwang.pinhaohuo.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Group;
import com.xunshangwang.pinhaohuo.transformation.CircleTransformation;
import com.xunshangwang.pinhaohuo.widgets.ProgressBar;

import java.util.BitSet;
import java.util.List;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/2/29.
 */
public class GroupAdapter extends BaseRecyclerAdapter<Group>{
    private BitSet mAnimatedSet;
    public GroupAdapter(Context context) {
        super(context);
        mAnimatedSet=new BitSet();
    }

    @Override
    public void setList(List<Group> list) {
        mAnimatedSet.clear();
        super.setList(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.group_item,parent,false);
        return new GroupHolder(view);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        GroupHolder groupHolder= (GroupHolder) holder;
        Group group=getList().get(position);
        Glide.with(getContext()).load(group.getOrder_goods().getImage_url()).centerCrop()
                .into(groupHolder.goodsImage);
        Glide.with(getContext()).load(group.getHeadImgUrl()).transform(new CircleTransformation(getContext()))
                .into(groupHolder.leaderImage);
        groupHolder.goodsNameText.setText(group.getOrder_goods().getGoods_name());
        groupHolder.leaderNameText.setText("团长: "+group.getGroup_nickname());
        groupHolder.idText.setText(String.valueOf(group.getGroup_order_id()));
        long interval=group.getExpire_time()-group.getCurrent_time();
        if(interval<0)
            groupHolder.lastTimeText.setText("已过期");
        else if(interval<60*60)
            groupHolder.lastTimeText.setText("即将结束");
        else if(interval<60*60*3)
            groupHolder.lastTimeText.setText("小于3小时");
        else if(interval<60*60*6)
            groupHolder.lastTimeText.setText("小于6小时");
        else if(interval<60*60*12)
            groupHolder.lastTimeText.setText("小于12小时");
        else
            groupHolder.lastTimeText.setText("大于12小时");
        int lastPeople=group.getOrder_goods().getGroup_number()-group.getPeople();

        if(lastPeople<=0){
            groupHolder.groupNumText.setText("拼团成功");
        }
        else {
            groupHolder.groupNumText.setText("距离成功还差"+lastPeople+"人");
        }
        groupHolder.progressBar.invalidateData(group.getPeople(), group.getOrder_goods().getGroup_number(), group.getStatus() == 2, !mAnimatedSet.get(position));
        mAnimatedSet.set(position);
        switch (group.getStatus()){
            case 0:
                groupHolder.statusImage.setImageDrawable(null);
                break;
            case 1:
                groupHolder.statusImage.setImageResource(R.drawable.group_success_image);
                break;
            case 2:
                groupHolder.statusImage.setImageResource(R.drawable.group_fail_image);
                break;
        }
    }
    private class GroupHolder extends ClickableViewHolder{
        ImageView goodsImage,leaderImage;
        TextView leaderNameText,goodsNameText,lastTimeText,
            groupNumText;
        ProgressBar progressBar;
        ImageView statusImage;
        TextView idText;
        public GroupHolder(View itemView) {
            super(itemView);
            goodsImage= (ImageView) itemView.findViewById(R.id.goods_image);
            leaderImage= (ImageView) itemView.findViewById(R.id.group_leader_head_image);
            leaderNameText= (TextView) itemView.findViewById(R.id.group_leader_name_text);
            goodsNameText= (TextView) itemView.findViewById(R.id.goods_name_text);
            lastTimeText= (TextView) itemView.findViewById(R.id.group_last_time_text);
            groupNumText= (TextView) itemView.findViewById(R.id.group_num_text);
            progressBar= (ProgressBar) itemView.findViewById(R.id.group_num_progress_bar);
            statusImage= (ImageView) itemView.findViewById(R.id.group_status_image);
            idText= (TextView) itemView.findViewById(R.id.group_id_text);
        }
    }
}
